//
//  ScanOverViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/25.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "ScanOverViewController.h"
#import "ScanIdList.h"
#import "ScanIdModel.h"
#import "ScanBandModel.h"
#import "ZHFAddTitleAddressView.h"
#import "ConfirmSuccessViewController.h"
#import "RealNameParametersModel.h"
#import "ScanIdStore.h"

@interface ScanOverViewController ()<ZHFAddTitleAddressViewDelegate>
{
    
    __weak IBOutlet UIView *bottomView;
    __weak IBOutlet UITextField *userName;
    __weak IBOutlet UITextField *userId;
    
    __weak IBOutlet UITextField *bandId;
    __weak IBOutlet UITextField *bandName;
    
    __weak IBOutlet UILabel *addressLa;
    __weak IBOutlet UITextField *bandTf;
    
    __weak IBOutlet UIImageView *idFontIm;
    __weak IBOutlet UIImageView *idBackIm;
    __weak IBOutlet UIImageView *headerIm;
    __weak IBOutlet UIImageView *bandFontIm;
}

@property(nonatomic,strong)ZHFAddTitleAddressView * addTitleAddressView;
@property (nonatomic,weak) UIButton *btn;
@end

@implementation ScanOverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    bottomView.layer.shadowColor = [UIColor blackColor].CGColor;
//    bottomView.layer.shadowOpacity = 0.2f;
//    bottomView.layer.shadowRadius = 4.f;
//    bottomView.layer.shadowOffset = CGSizeMake(1,1);
    
    [self setNavBarWithTitle:@"实名认证"];
    
    [self setUI];
    
    [self setData];
    
}

- (void)setData{
    
    userName.text = [ScanIdList sharedInstance].scanModel.name;
    userId.text = [ScanIdList sharedInstance].scanModel.code;
    
    bandId.text = [ScanIdList sharedInstance].scanBandModel.bankNumber;
    bandName.text = [ScanIdList sharedInstance].scanBandModel.bankName;
    
    idFontIm.image = [ScanIdList sharedInstance].scanModel.idFontImage;
    idBackIm.image = [ScanIdList sharedInstance].scanModel.idBackImage;
    headerIm.image = [ScanIdList sharedInstance].scanModel.photoImgage;
    bandFontIm.image = [ScanIdList sharedInstance].scanBandModel.bankImage;
}


-(void)setUI{
    self.addTitleAddressView = [[ZHFAddTitleAddressView alloc]init];
    self.addTitleAddressView.title = @"选择地址";
    self.addTitleAddressView.userID = 7;
    self.addTitleAddressView.delegate1 = self;
    self.addTitleAddressView.defaultHeight = 350;
    self.addTitleAddressView.titleScrollViewH = 37;
    [self.view addSubview:[self.addTitleAddressView initAddressView]];
}

#pragma mark - 选择地址
- (IBAction)onSelectAdBtn:(UIButton *)sender {
    
    [self.addTitleAddressView addAnimate];
    
}

#pragma mark - ZHFAddTitleAddressViewDelegate
-(void)cancelBtnClick:(NSString *)titleAddress titleID:(NSString *)titleID{
    
    if (titleID.length <= 0) {
        
        return;
    }
    
    addressLa.text = titleAddress;
    [ScanIdList sharedInstance].realNameModel.region = titleID;
    [ScanIdList sharedInstance].realNameModel.bankadd = titleID;
    NSLog( @"%@", [NSString stringWithFormat:@"打印的对应省市县的id=%@",titleID]);
}


#pragma mark - 确认
- (IBAction)onPostBtn:(UIButton *)sender {
    
    if (userName.text.length <= 0) {
        
        return [self showMBPError:@"请输入姓名"];
    }
    
    [ScanIdList sharedInstance].realNameModel.name = userName.text;
    
    if (userId.text.length <= 0) {
        
        return [self showMBPError:@"请输入身份证号"];
    }
    
    [ScanIdList sharedInstance].realNameModel.idCardNumber = userId.text;
    
    if (bandId.text.length <= 0) {
        
        return [self showMBPError:@"请输入银行卡号"];
    }
    
    [ScanIdList sharedInstance].realNameModel.bankno = bandId.text;
    
    if (bandName.text.length <= 0) {
        
        return [self showMBPError:@"请输入结算银行"];
    }
    
    [ScanIdList sharedInstance].realNameModel.bank = bandName.text;
    
    if (addressLa.text.length <= 0 || [addressLa.text isEqualToString:@"请选择地区"]) {
        
        return [self showMBPError:@"选择所属省市"];
    }
    
    NSArray *arr = [[ScanIdList sharedInstance].realNameModel.region componentsSeparatedByString:@" "];
    
    if (arr.count == 2) {
        
        return [self showMBPError:@"请选择完成区域位置"];
    }
    
    if (bandTf.text.length <= 0) {
        
        return [self showMBPError:@"填写结算开户行"];
    }
    
    sender.enabled = NO;
    self.btn = sender;
    
    [ScanIdList sharedInstance].realNameModel.khzh = bandTf.text;
    
    [SVProgressHUD show];
    MJWeakSelf
    ScanIdStore *store = [[ScanIdStore alloc] init];
    [store postRealNameWithParameters:[ScanIdList sharedInstance].realNameModel Success:^{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"REALNAMEING" object:nil];
        
        [SVProgressHUD dismiss];
        ConfirmSuccessViewController *VC = [[ConfirmSuccessViewController alloc] init];
        [self.navigationController pushViewController:VC animated:YES];
        
    } Failure:^(NSError *error) {
        
        sender.enabled = YES;
        [SVProgressHUD dismiss];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    self.btn.enabled = YES;
}
@end
